package com.francis.springbootcommonutil.encry;

import org.apache.commons.codec.binary.Hex;
import org.springframework.util.StringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * SHA算法， 固定长度摘要算法， 无密钥， 速度快
 */
public class SHA {

    private static final String ALGORITHM_1 = "SHA";
    private static final String ALGORITHM_224 = "SHA-224";
    private static final String ALGORITHM_256 = "SHA-256";
    private static final String ALGORITHM_384 = "SHA-384";
    private static final String ALGORITHM_512 = "SHA-512";

    /**
     * SHA1加密
     * @param data
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String encryptSHA(String data) {
        if(StringUtils.isEmpty(data)){
            throw new NullPointerException("请输入待加密字段");
        }
        return encrypt(data, ALGORITHM_1);
    }

    /**
     * SHA224加密
     * @param data
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String encryptSHA224(String data) {
        if(StringUtils.isEmpty(data)){
            throw new NullPointerException("请输入待加密字段");
        }
        return encrypt(data, ALGORITHM_224);
    }

    /**
     * SHA256加密
     * @param data
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String encryptSHA256(String data) {
        if(StringUtils.isEmpty(data)){
            throw new NullPointerException("请输入待加密字段");
        }
        return encrypt(data, ALGORITHM_256);
    }

    /**
     * SHA384加密
     * @param data
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String encryptSHA384(String data) {
        if(StringUtils.isEmpty(data)){
            throw new NullPointerException("请输入待加密字段");
        }
        return encrypt(data, ALGORITHM_384);
    }

    /**
     * SHA512加密
     * @param data
     * @return
     */
    public static String encryptSHA512(String data) {
        if(StringUtils.isEmpty(data)){
            throw new NullPointerException("请输入待加密字段");
        }
        return encrypt(data, ALGORITHM_512);
    }

    private static String encrypt(String data, String algorithm)  {
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        digest.update(data.getBytes());
        return Hex.encodeHexString(digest.digest());
    }

    public static void main(String[] args)throws Exception {
        String data = "12345";
        String sha = encryptSHA(data);
        String sha224 = encryptSHA224(data);
        String sha256 = encryptSHA256(data);
        String sha384 = encryptSHA384(data);
        String sha512 = encryptSHA512(data);

        System.out.println("SHA: " + sha);
        System.out.println("SHA224: " + sha224);
        System.out.println("SHA256: " + sha256);
        System.out.println("SHA384: " + sha384);
        System.out.println("SHA512: " + sha512);
    }


}
